//
//  AnswerScene.m
//  DareMemory
//
//  Created by caozp on 09-12-1.
//  Copyright 2009 pla. All rights reserved.
//

#import "NumberAnswerScene.h"
#import "NumberGameScene.h"
#import "SimpleAudioEngine.h"

extern int nNumberLevel;
extern int nNumberNumber;
extern int nNumberValue;

extern BOOL isSound;

@implementation NumberAnswerScene

+(id) scene
{
	// 'scene' is an autorelease object.
	Scene *scene = [Scene node];
	
	// 'layer' is an autorelease object.
	NumberAnswerScene *layer = [NumberAnswerScene node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;	
}

-(id) init
{
	if( (self=[super init] )) {
		answer = -1;
		numberCount = 0;
		
		Sprite *background = [Sprite spriteWithFile:@"background.png"];
		[background setPosition:ccp(240, 160)];
		[self addChild:background z:0];
		
		BitmapFontAtlas *label00 = [BitmapFontAtlas bitmapFontAtlasWithString:@"0" fntFile:@"bitmapFontTest.fnt"];
		MenuItem *item0 = [MenuItemLabel itemWithLabel:label00 target:self selector:@selector(Answer0:)];
		BitmapFontAtlas *label01 = [BitmapFontAtlas bitmapFontAtlasWithString:@"1" fntFile:@"bitmapFontTest.fnt"];
		MenuItem *item1 = [MenuItemLabel itemWithLabel:label01 target:self selector:@selector(Answer1:)];
		BitmapFontAtlas *label02 = [BitmapFontAtlas bitmapFontAtlasWithString:@"2" fntFile:@"bitmapFontTest.fnt"];
		MenuItem *item2 = [MenuItemLabel itemWithLabel:label02 target:self selector:@selector(Answer2:)];
		BitmapFontAtlas *label03 = [BitmapFontAtlas bitmapFontAtlasWithString:@"3" fntFile:@"bitmapFontTest.fnt"];
		MenuItem *item3 = [MenuItemLabel itemWithLabel:label03 target:self selector:@selector(Answer3:)];
		BitmapFontAtlas *label04 = [BitmapFontAtlas bitmapFontAtlasWithString:@"4" fntFile:@"bitmapFontTest.fnt"];
		MenuItem *item4 = [MenuItemLabel itemWithLabel:label04 target:self selector:@selector(Answer4:)];
		BitmapFontAtlas *label05 = [BitmapFontAtlas bitmapFontAtlasWithString:@"5" fntFile:@"bitmapFontTest.fnt"];
		MenuItem *item5 = [MenuItemLabel itemWithLabel:label05 target:self selector:@selector(Answer5:)];
		BitmapFontAtlas *label06 = [BitmapFontAtlas bitmapFontAtlasWithString:@"6" fntFile:@"bitmapFontTest.fnt"];
		MenuItem *item6 = [MenuItemLabel itemWithLabel:label06 target:self selector:@selector(Answer6:)];
		BitmapFontAtlas *label07 = [BitmapFontAtlas bitmapFontAtlasWithString:@"7" fntFile:@"bitmapFontTest.fnt"];
		MenuItem *item7 = [MenuItemLabel itemWithLabel:label07 target:self selector:@selector(Answer7:)];
		BitmapFontAtlas *label08 = [BitmapFontAtlas bitmapFontAtlasWithString:@"8" fntFile:@"bitmapFontTest.fnt"];
		MenuItem *item8 = [MenuItemLabel itemWithLabel:label08 target:self selector:@selector(Answer8:)];
		BitmapFontAtlas *label09 = [BitmapFontAtlas bitmapFontAtlasWithString:@"9" fntFile:@"bitmapFontTest.fnt"];
		MenuItem *item9 = [MenuItemLabel itemWithLabel:label09 target:self selector:@selector(Answer9:)];
		Menu *menu = [Menu menuWithItems:item0, item1, item2, item3, item4, item5, item6, item7, item8, item9, nil];
		//[menu alignItemsHorizontally];
		[menu alignItemsHorizontallyWithPadding:25];
		menu.position = ccp(240, 180);
		[self addChild:menu z:0 tag:2];
		
		NSString *askString = [NSString stringWithFormat:@"The No.%d is", nNumberNumber];
//		Label *askLabel = [Label labelWithString:askString fontName:@"Marker Felt" fontSize:32];
		BitmapFontAtlas* askLabel = [BitmapFontAtlas bitmapFontAtlasWithString:askString fntFile:@"bitmapFontTest.fnt"];
		askLabel.position = ccp(240, 240);
		[self addChild:askLabel];
		
//		Label *label1 = [Label labelWithString:@"" fontName:@"Marker Felt" fontSize:48];
		BitmapFontAtlas* label1 = [BitmapFontAtlas bitmapFontAtlasWithString:@"" fntFile:@"bitmapFontTest.fnt"];
		label1.position = ccp(240, 160);
		[self addChild:label1 z:0 tag:1];

	}
	return self;	
}

- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}

- (void) Answer0: (id) sender
{
	if (nNumberValue == 0) {
		isRight = YES;
	}
	else {
		isRight = NO;
	}
	Menu *menu = (Menu*)[self getChildByTag:2];
	[menu setVisible:NO];
	[self schedule:@selector(step:) interval: 2.0f];
}

- (void) Answer1: (id) sender
{
	if (nNumberValue == 1) {
		isRight = YES;
	}
	else {
		isRight = NO;
	}
	Menu *menu = (Menu*)[self getChildByTag:2];
	[menu setVisible:NO];
	[self schedule:@selector(step:) interval: 2.0f];
}

- (void) Answer2: (id) sender
{
	if (nNumberValue == 2) {
		isRight = YES;
	}
	else {
		isRight = NO;
	}
	Menu *menu = (Menu*)[self getChildByTag:2];
	[menu setVisible:NO];
	[self schedule:@selector(step:) interval: 2.0f];
}

- (void) Answer3: (id) sender
{
	if (nNumberValue == 3) {
		isRight = YES;
	}
	else {
		isRight = NO;
	}
	Menu *menu = (Menu*)[self getChildByTag:2];
	[menu setVisible:NO];
	[self schedule:@selector(step:) interval: 2.0f];
}

- (void) Answer4: (id) sender
{
	if (nNumberValue == 4) {
		isRight = YES;
	}
	else {
		isRight = NO;
	}
	Menu *menu = (Menu*)[self getChildByTag:2];
	[menu setVisible:NO];
	[self schedule:@selector(step:) interval: 2.0f];
}

- (void) Answer5: (id) sender
{
	if (nNumberValue == 5) {
		isRight = YES;
	}
	else {
		isRight = NO;
	}
	Menu *menu = (Menu*)[self getChildByTag:2];
	[menu setVisible:NO];
	[self schedule:@selector(step:) interval: 2.0f];
}

- (void) Answer6: (id) sender
{
	if (nNumberValue == 6) {
		isRight = YES;
	}
	else {
		isRight = NO;
	}
	Menu *menu = (Menu*)[self getChildByTag:2];
	[menu setVisible:NO];
	[self schedule:@selector(step:) interval: 2.0f];
}

- (void) Answer7: (id) sender
{
	if (nNumberValue == 7) {
		isRight = YES;
	}
	else {
		isRight = NO;
	}
	Menu *menu = (Menu*)[self getChildByTag:2];
	[menu setVisible:NO];
	[self schedule:@selector(step:) interval: 2.0f];
}

- (void) Answer8: (id) sender
{
	if (nNumberValue == 8) {
		isRight = YES;
	}
	else {
		isRight = NO;
	}
	Menu *menu = (Menu*)[self getChildByTag:2];
	[menu setVisible:NO];
	[self schedule:@selector(step:) interval: 2.0f];
}

- (void) Answer9: (id) sender
{
	if (nNumberValue == 9) {
		isRight = YES;
	}
	else {
		isRight = NO;
	}
	Menu *menu = (Menu*)[self getChildByTag:2];
	[menu setVisible:NO];
	[self schedule:@selector(step:) interval: 2.0f];
}

-(void) step:(ccTime) dt
{
	numberCount++;
	if (numberCount == 1) {
		Label *label1 = (Label*)[self getChildByTag:1];
		if (isRight == YES) {
			Sprite *flower1 = [Sprite spriteWithFile:@"flower.png"];
			flower1.position = ccp(96, 360);
			[self addChild:flower1];
			id rotateAction1 = [RotateBy actionWithDuration:8 angle:180*4];
			id moveAction1 = [MoveTo actionWithDuration:4 position:ccp(96, -80)];
			id spawn1 = [Spawn actions:rotateAction1, moveAction1, nil];
			[flower1 runAction:spawn1];
			
			Sprite *flower2 = [Sprite spriteWithFile:@"flower.png"];
			flower2.position = ccp(192, 360);
			[self addChild:flower2];
			id rotateAction2 = [RotateBy actionWithDuration:8 angle:-180*4];
			id moveAction2 = [MoveTo actionWithDuration:3.5 position:ccp(192, -80)];
			id spawn2 = [Spawn actions:rotateAction2, moveAction2, nil];
			[flower2 runAction:spawn2];
			
			Sprite *flower3 = [Sprite spriteWithFile:@"flower.png"];
			flower3.position = ccp(288, 360);
			[self addChild:flower3];
			id rotateAction3 = [RotateBy actionWithDuration:8 angle:180*4];
			id moveAction3 = [MoveTo actionWithDuration:4.5 position:ccp(288, -80)];
			id spawn3 = [Spawn actions:rotateAction3, moveAction3, nil];
			[flower3 runAction:spawn3];
			
			Sprite *flower4 = [Sprite spriteWithFile:@"flower.png"];
			flower4.position = ccp(384, 360);
			[self addChild:flower4];
			id rotateAction4 = [RotateBy actionWithDuration:8 angle:-180*4];
			id moveAction4 = [MoveTo actionWithDuration:4 position:ccp(384, -80)];
			id spawn4 = [Spawn actions:rotateAction4, moveAction4, nil];
			[flower4 runAction:spawn4];
			
			nNumberLevel++;
			[label1 setString:@"Right!"];
			if (isSound == YES) {
				[[SimpleAudioEngine sharedEngine] playEffect:@"win.wav"];
			}
		}
		else {
			Sprite *flower1 = [Sprite spriteWithFile:@"bird.png"];
			flower1.position = ccp(96, 360);
			[self addChild:flower1];
			id rotateAction1 = [RotateBy actionWithDuration:8 angle:180*4];
			id moveAction1 = [MoveTo actionWithDuration:4 position:ccp(96, -80)];
			id spawn1 = [Spawn actions:rotateAction1, moveAction1, nil];
			[flower1 runAction:spawn1];
			
			Sprite *flower2 = [Sprite spriteWithFile:@"bird.png"];
			flower2.position = ccp(192, 360);
			[self addChild:flower2];
			id rotateAction2 = [RotateBy actionWithDuration:8 angle:-180*4];
			id moveAction2 = [MoveTo actionWithDuration:3.5 position:ccp(192, -80)];
			id spawn2 = [Spawn actions:rotateAction2, moveAction2, nil];
			[flower2 runAction:spawn2];
			
			Sprite *flower3 = [Sprite spriteWithFile:@"bird.png"];
			flower3.position = ccp(288, 360);
			[self addChild:flower3];
			id rotateAction3 = [RotateBy actionWithDuration:8 angle:180*4];
			id moveAction3 = [MoveTo actionWithDuration:4.5 position:ccp(288, -80)];
			id spawn3 = [Spawn actions:rotateAction3, moveAction3, nil];
			[flower3 runAction:spawn3];
			
			Sprite *flower4 = [Sprite spriteWithFile:@"bird.png"];
			flower4.position = ccp(384, 360);
			[self addChild:flower4];
			id rotateAction4 = [RotateBy actionWithDuration:8 angle:-180*4];
			id moveAction4 = [MoveTo actionWithDuration:4 position:ccp(384, -80)];
			id spawn4 = [Spawn actions:rotateAction4, moveAction4, nil];
			[flower4 runAction:spawn4];
			
			[label1 setString:@"Wrong!"];
			if (isSound) {
				[[SimpleAudioEngine sharedEngine] playEffect:@"lose.wav"];
			}
		}
	}
	if (numberCount == 2) {
		Label *label1 = (Label*)[self getChildByTag:1];
		if (isRight == YES) {
			[label1 setString:@"Play next level!"];
		}
		else {
			[label1 setString:@"Please try again!"];
		}
	}
	if (numberCount == 3) {
		[[Director sharedDirector] replaceScene:[FlipXTransition transitionWithDuration:1 scene:[NumberGameScene node]]];
	}
}

@end
